<template>
    <div>
        <table border="1" align="center" width="500px">
            <caption>欢迎 {{user.username}} 进入本餐厅, 您是 {{user.level | level}}</caption>
            <tr>
                <td>名称</td>
                <td>价格</td>
                <td>操作</td>
            </tr>
            <tr v-for="goods in goodsList" v-show="user.level<=goods.level">
                <td>{{goods.title}}</td>
                <td>{{goods.price}}</td>
<!--                <td v-if="goods.id==user.id"><button @click="postRecord(goods.id)">开吃</button></td>-->
<!--                <td v-else><button @click="putRecord(goods.id)">结束</button></td>-->
                <td><button @click="postRecord(goods.id)">开吃</button></td>
                <td><button @click="putRecord(goods.id)">结束</button></td>
            </tr>
        </table>
        <li v-if="user.level==0"><button @click="getUser">最有钱会员</button><button @click="getGoods">最畅销套餐</button></li>
    </div>
</template>

<script>
    export default {
        name: "index_app1",
        data() {
            return {
                user: JSON.parse(sessionStorage.getItem('user')),
                goodsList: [],
                userList: [],
                RecordList: [],
            }
        },
        created() {
            this.$axios({
                    url: 'app1/goods/',
                }).then(resp=>{
                    this.goodsList = resp.data
                })
        },
        methods: {
            getUser(){
                 this.$axios({
                    url: 'app1/user/',
                }).then(resp=>{
                    this.userList = resp.data
                })
            },
            getGoods(){
                 this.$axios({
                    url: 'app1/record/',
                }).then(resp=>{
                    this.RecordList = resp.data
                })
            },
            postRecord(id){
                this.$axios({
                    url: 'app1/record/',
                    method: 'post',
                    data: {
                        user_id: this.user.id,
                        goods_id: id
                    }
                }).then(resp=>{
                    alert(resp.data.msg);
                    if (resp.data.code==200){
                        this.$router.go(0)
                    }
                })
            },
            putRecord(id){
                this.$axios({
                    url: 'app1/record/',
                    method: 'put',
                    data: {
                        user_id: this.user.id,
                        goods_id: id
                    }
                }).then(resp=>{
                    console.log(resp.data)
                })
            },

        },
        computed: {},
        filters: {
            level(x){
                if (x == 0){
                    return '超级管理员'
                }else if (x == 1) {
                    return '高级会员'
                }else {
                    return '普通会员'
                }
            }
        },
        watch: {},
    }
</script>

<style scoped>

</style>
